package com.my.study.algorithm.leetcode;

/**
 * 长度最小子数组
 * <p>
 *     https://leetcode.cn/problems/minimum-size-subarray-sum/description/
 * </p>
 * 
 * @author Carlos
 * @version 1.0.0
 * @date 2023/4/7 8:21
 */
public class MinSubArrayLen {


    public static void main(String[] args) {
        
    }
    
    
    private static int minSubArrLen(int[] arr, int target) {
        int len = arr.length;
        int ans = len + 1;
        int sum = 0;
        int left = 0;

        for (int right = 0; right < len; right++) {
            sum += arr[right];
            
            while (sum >= target) {
                ans = Math.min(ans, right - left + 1);
                sum -= arr[left];
                left++;
            }
        }
        
        
        return ans <= len ? ans : 0;
    }
    
}
